
#include "student.h"
#include<istream>
#include<string>
#include<iostream>
#include "collegestudent.h"

using namespace std;

void CollegeStudent::input() {
    cout << "请输入学号: ";
    getline(cin, studentId);
    cout << "请输入姓名: ";
    getline(cin, name);
    cout << "请输入性别(M/F): ";
    cin >> gender;
    cout << "请输入年龄: ";
    cin >> age;
    cin.ignore();
    cout << "请输入班级: ";
    getline(cin, className);
    cout << "请输入专业: ";
    getline(cin, major);
    
    cout << "请输入英语成绩: ";
    cin >> english;
    cout << "请输入程序设计成绩: ";
    cin >> programming;
    cout << "请输入高等数学成绩: ";
    cin >> advancedMath;
    cin.ignore();
}

void CollegeStudent::display() const {
    cout << "学号: " << studentId << endl;
    cout << "姓名: " << name << endl;
    cout << "性别: " << gender << endl;
    cout << "年龄: " << age << endl;
    cout << "班级: " << className << endl;
    cout << "专业: " << major << endl;
    cout << "英语成绩: " << english << endl;
    cout << "程序设计成绩: " << programming << endl;
    cout << "高等数学成绩: " << advancedMath << endl;
    cout << "总分: " << calculateTotalScore() << endl;
}

double CollegeStudent::calculateTotalScore() const {
    return english + programming + advancedMath;
}

void CollegeStudent::save(ofstream& out) const
{
    // 写入学生类型
    string type = "CollegeStudent";
    out.write(type.c_str(), type.size() + 1);

    // 先保存基类（MiddleStudent）的数据
    //Student::save(out);

    // 写入基本信息
    out.write(studentId.c_str(), studentId.size() + 1);
    out.write(name.c_str(), name.size() + 1);
    out.write(&gender, sizeof(gender));
    out.write(reinterpret_cast<const char*>(&age), sizeof(age));
    out.write(className.c_str(), className.size() + 1);
    out.write(major.c_str(), major.size() + 1);

    // 写入成绩
    out.write(reinterpret_cast<const char*>(&english), sizeof(english));
    out.write(reinterpret_cast<const char*>(&programming), sizeof(programming));
    out.write(reinterpret_cast<const char*>(&advancedMath), sizeof(advancedMath));
}

void CollegeStudent::load(ifstream& in)
{
    char buffer[256];

    // 先加载基类（PrimaryStudent）的数据
    //PrimaryStudent::load(in);

    // 读取基本信息
    in.getline(buffer, 256, '\0');
    studentId = buffer;

    in.getline(buffer, 256, '\0');
    name = buffer;

    in.read(&gender, sizeof(gender));
    in.read(reinterpret_cast<char*>(&age), sizeof(age));

    in.getline(buffer, 256, '\0');
    className = buffer;

    in.getline(buffer, 256, '\0');
    major = buffer;

    // 读取成绩
    in.read(reinterpret_cast<char*>(&english), sizeof(english));
    in.read(reinterpret_cast<char*>(&programming), sizeof(programming));
    in.read(reinterpret_cast<char*>(&advancedMath), sizeof(advancedMath));
}
